package com.fancy.learncenter.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.activity.base.MallCommonH5Activity;
import com.fancy.learncenter.ui.fragment.CartoonTeacherClassTotalReportFragment;
import com.fancy.learncenter.ui.fragment.CartoonTeacherClassWeekReportFragment;
import com.fancy.learncenter.ui.view.SharePopupWindow;
import com.fancy.learncenter.utils.DataUtil;
import com.fancy.learncenter.utils.WXShareUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.fancy.learncenter.ui.activity.CartoonTeacherClassDetailActivity.CLASS_ID_KEY;

public class CartoonTeacherClassReportActivity extends BaseActivity {


    String classId = "";
    @Bind(R.id.week_report)
    TextView weekReport;
    @Bind(R.id.total_report)
    TextView totalReport;
    @Bind(R.id.rootView)
    LinearLayout rootView;


    String picUrl = "";
    String title = "【环球少年】本周作业报告";
    String content = "";
    String likUrl = "http://mall.fancyedu.com/rct/activityOnline/weekreport?classId=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_teacher_class_report);
        ButterKnife.bind(this);
        initToolbar("学习情况");
        setRightImg(R.mipmap.cartoon_report_share_icon, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sharePopupWindow == null) {
                    sharePopupWindow = new SharePopupWindow(CartoonTeacherClassReportActivity.this, rootView);

                    sharePopupWindow.setShareCallBack(new SharePopupWindow.ShareCallBack() {
                        @Override
                        public void wxCallBack() {
                            WXShareUtil.getInstance(CartoonTeacherClassReportActivity.this).shareWebPage(thumbBmp, title, content, likUrl, false);
                        }

                        @Override
                        public void wxFriendCallBack() {
                            WXShareUtil.getInstance(CartoonTeacherClassReportActivity.this).shareWebPage(thumbBmp, title, content, likUrl, true);
                        }
                    });
                }
                sharePopupWindow.showPopupWindow();
            }
        });


        weekReport.setText("本周（" + DataUtil.getCutWeek("MM.dd") + "）");
        classId = getIntent().getStringExtra(CLASS_ID_KEY);
        fragmentManager = getSupportFragmentManager();


        content = DataUtil.getCutWeek();

        likUrl = likUrl + classId;

        thumbBmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.share_default), 150, 150, true);

        showIndex(1);
    }

    Bitmap thumbBmp;

    SharePopupWindow sharePopupWindow;

    FragmentManager fragmentManager;
    FragmentTransaction transaction;

    CartoonTeacherClassWeekReportFragment weekReportFragment;
    CartoonTeacherClassTotalReportFragment totalReportFragment;

    private void showIndex(int index) {

        transaction = fragmentManager.beginTransaction();
        switch (index) {
            case 2:
                if (totalReportFragment == null) {
                    totalReportFragment = CartoonTeacherClassTotalReportFragment.newInstance(classId);
                    transaction.add(R.id.fg_container, totalReportFragment);
                }

                if (weekReportFragment != null) {
                    transaction.hide(weekReportFragment);
                }

                transaction.show(totalReportFragment);
                transaction.commit();

                break;
            case 1:
                if (weekReportFragment == null) {
                    weekReportFragment = CartoonTeacherClassWeekReportFragment.newInstance(classId);
                    transaction.add(R.id.fg_container, weekReportFragment);
                }

                if (totalReportFragment != null) {
                    transaction.hide(totalReportFragment);
                }

                transaction.show(weekReportFragment);
                transaction.commit();
                break;
        }
    }

    @OnClick({R.id.week_report, R.id.total_report})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.week_report:
                showIndex(1);
                setRightImgShow();
                totalReport.setBackground(null);
                totalReport.setTextColor(Color.parseColor("#FFFFFF"));

                weekReport.setBackgroundResource(R.drawable.shape_white_bt);
                weekReport.setTextColor(Color.parseColor("#079ea8"));
                break;
            case R.id.total_report:

                setRightImgHide();
                showIndex(2);


                weekReport.setBackground(null);
                weekReport.setTextColor(Color.parseColor("#FFFFFF"));

                totalReport.setBackgroundResource(R.drawable.shape_white_bt);
                totalReport.setTextColor(Color.parseColor("#079ea8"));
                break;
        }
    }
}
